package com.example.aabb;

import android.graphics.Color;

import com.example.aabb.Bill;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import java.util.ArrayList;
import java.util.List;


public class BarChartHelper {
    public static void generateBarChart(BarChart barChart, List<Bill2> billList, String type) {
        List<BarEntry> entries = new ArrayList<>();
        List<String> xAxisLabels = new ArrayList<>();

        double total = 0;

        if ("支出".equals(type)) {
            for (int i = 0; i < billList.size(); i++) {
                Bill2 bill = billList.get(i);
                double amount = bill.getAmount();
                if (amount < 0) {
                    entries.add(new BarEntry(i, (float) Math.abs(amount)));
                    total += Math.abs(amount);
                    xAxisLabels.add(bill.getmDate());
                }
            }
        } else if ("收入".equals(type)) {
            for (int i = 0; i < billList.size(); i++) {
                Bill2 bill = billList.get(i);
                double amount = bill.getAmount();
                if (amount > 0) {
                    entries.add(new BarEntry(i, (float) amount));
                    total += amount;
                    xAxisLabels.add(bill.getmDate());
                }
            }
        } else {
            // 处理其他类型（如果有，目前这里暂未详细实现，可按需扩展）
            return;
        }

        BarDataSet dataSet = new BarDataSet(entries, type + "金额");
        dataSet.setColors(Color.GREEN);
        dataSet.setValueTextColor(Color.BLACK);

        BarData barData = new BarData(dataSet);
        barChart.setData(barData);

        XAxis xAxis = barChart.getXAxis();
        xAxis.setValueFormatter(new IndexAxisValueFormatter(xAxisLabels));
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);

        YAxis leftAxis = barChart.getAxisLeft();
        leftAxis.setDrawGridLines(false);

        YAxis rightAxis = barChart.getAxisRight();
        rightAxis.setDrawGridLines(false);

        barChart.getDescription().setEnabled(false);
        barChart.animateY(1000);
        barChart.invalidate();
    }
}